package com.naton.mydemo.ui.act

import android.os.Bundle
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.naton.common.ui.BaseVMActivity
import com.naton.common.ui.BaseVBFragment
import com.naton.mydemo.R
import com.naton.mydemo.bean.TabEntity
import com.naton.mydemo.databinding.ActivityMainBinding
import com.naton.mydemo.ui.fragment.HomeFragment
import com.naton.mydemo.ui.fragment.MeFragment
import com.naton.mydemo.ui.fragment.MessageFragment
import com.naton.mydemo.viewmodel.MainViewModel
import java.util.ArrayList

class MainActivity : BaseVMActivity<ActivityMainBinding , MainViewModel>() {

    private var messageFragment: MessageFragment? = null
    private var homeFragment: HomeFragment? = null
    private var meFragment: MeFragment? = null

    //当前在显示的fragment
    private var currFragment: BaseVBFragment<*>? = null
    val mTitles = arrayOf("消息", "主页", "我的")
    private val mTabEntities = ArrayList<CustomTabEntity>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
        addListener()
    }

    private fun initView() {
        for (i in mTitles.indices) {
            mTabEntities.add(
                TabEntity(
                    mTitles[i],
                    R.mipmap.icon_message_selected,
                    R.mipmap.icon_message
                )
            )
        }
        mBinding.mTabLayout.setTabData(mTabEntities)
        //默认选中消息

        if (messageFragment == null) {
            messageFragment = MessageFragment()
        }
        changeFragment(messageFragment)
    }

    private fun addListener() {
        mBinding?.mTabLayout?.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabReselect(position: Int) {}

            override fun onTabSelect(position: Int) {
                when (position) {
                    0 -> {
                        if (messageFragment == null) {
                            messageFragment = MessageFragment()
                        }
                        changeFragment(messageFragment)
                    }
                    1 -> {
                        //判断是否有Crm权限
                        if (homeFragment == null) {
                            homeFragment = HomeFragment()
                        }
                        changeFragment(homeFragment)
                    }
                    2 -> {
                        if (meFragment == null) {
                            meFragment = MeFragment()
                        }
                        changeFragment(meFragment)
                    }
                }
            }
        })
    }

    /**
     * 切换页面
     */
    private fun changeFragment(targetFragment: BaseVBFragment<*>?) {
        val transaction = supportFragmentManager.beginTransaction()
        if (targetFragment?.isAdded == false) {
            //第一次使用是判断currFragment是否为null
            if (currFragment != null) transaction.hide(currFragment!!)
            targetFragment.let { transaction.add(R.id.fragment, it) }
        } else {
            currFragment?.let { targetFragment?.let { it1 -> transaction.hide(it).show(it1) } }
        }
        currFragment = targetFragment
        transaction.commitAllowingStateLoss()
    }

}